package com.leador.assist;

import android.content.Context;
import android.util.Log;

import com.leador.jni.TraceJniInterface;
import com.leador.module.AppInfo;
import com.leador.module.ILocation;
import com.leador.module.TraceLocation;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 队列管理类
 * Created by liuha on 2016/9/15.
 */
public class DataQueueManager {
    public Deque realtimeDataQueue;
    public Deque bufferDataQueue;
    public  Map responseUploadTimes;
    public  Map uploadingDatas;
    public   Deque tempDataQueue;
    private  Deque<byte[]> sendDataQueue;
    public  Deque syncUpData;
    private int sendMaxLength = 10;

    private DataQueueManager(){
        realtimeDataQueue = new LinkedList();
        bufferDataQueue = new LinkedList();
        responseUploadTimes = new LinkedHashMap();
        uploadingDatas = new LinkedHashMap();
        tempDataQueue = new LinkedList();
        sendDataQueue = new LinkedList<byte[]>();
        syncUpData = new LinkedList();
    }
    private static DataQueueManager INSTANCE = null;
    public static DataQueueManager getInstance(){
        if(INSTANCE == null){
            synchronized (DataQueueManager.class){
                if(INSTANCE == null){
                    INSTANCE = new DataQueueManager();
                }
            }
        }
        return INSTANCE;
    }

    public Deque getRealtimeDataQueue() {
        return realtimeDataQueue;
    }

    public Deque getBufferDataQueue() {
        return bufferDataQueue;
    }

    public Map getResponseUploadTimes() {
        return responseUploadTimes;
    }

    public Map getUploadingDatas() {
        return uploadingDatas;
    }

    public Deque getTempDataQueue() {
        return tempDataQueue;
    }

//    public void remove(){
//
//    }

    private boolean dbHasValue = false;

    public boolean isDbHasValue() {
        return dbHasValue;
    }

    public void setDbHasValue(boolean dbHasValue) {
        this.dbHasValue = dbHasValue;
    }

    public void clearData(){
        realtimeDataQueue.clear();
        bufferDataQueue.clear();
        tempDataQueue.clear();
    }

    /**
     * 1.将数据加入发送缓存队列
     * @param bytes
     */
    public void addRealTimeDataQueue(byte[] bytes){
        if(bytes == null){
            return;
        }
        realtimeDataQueue.add(bytes);
    }


    /**
     * 准备发送数据
     */
    public byte[] setSendDataQueue(Context context){
        if(tempDataQueue.size()>0){
            tranQueue(tempDataQueue);
        }else if(!dbHasValue && bufferDataQueue.size() <= 0){
            tranQueue(realtimeDataQueue);
        }else {
            //同步数据库数据
            tranQueue(bufferDataQueue);
            if(bufferDataQueue.size() < 0){
                setDbHasValue(false);
            }
        }

        Log.e("sendDataQueue size",sendDataQueue.size()+"----");
//        byte[] var2 = new byte[e];
//        int var3 = sendDataQueue.size();
//        int i = 0;
//        for(int j = 0; j < var3; ++j) {
//            byte[] var5 = sendDataQueue.poll();
//            if(var5 != null) {
//                for(int k = 0; k < var5.length; ++k) {
//                    var2[i + k] = var5[k];
//                }
//                i += var5.length;
//                sendDataQueue.offer(var5);
//            }
//        }

        String json = null;
        byte[] var2 = new byte[e];
        int var3 = sendDataQueue.size();
        int i = 0;
        for(int j = 0; j < var3; ++j) {
            byte[] var5 = sendDataQueue.poll();
            if(var5 != null) {
                if(json == null){
                    json = new String(var5);
                }else{
                    json += ","+ new String(var5);
                }
                sendDataQueue.offer(var5);
            }
        }
        if(json == null){
            return null;
        }
        //绑定发送包
        TraceJniInterface.setSocketPackData(json.getBytes(),context);

        return TraceJniInterface.buildRequestData(context);
    }

    public void removeSendData(){
        while(DataQueueManager.getInstance().sendDataQueue.size() > 0) {
            byte[] var2 = sendDataQueue.poll();
        }
    }

    public Deque getSyncUpData() {
        return syncUpData;
    }

    public void setSyncUpData(Deque syncUpData) {
        this.syncUpData = syncUpData;
    }

    public void clearSyncUpData(){
        syncUpData.clear();
    }

    /**
     * 将发送队列队列加到缓存队列中去
     */
    public void sendToTempQueue() {
        while(true) {
            try {
                if(sendDataQueue.size() > 0) {
                    byte[] var0 = sendDataQueue.poll();
                    tempDataQueue.offerLast(var0);
                    continue;
                }
            } catch (Exception var1) {
                ;
            }
            e = 0;
            return;
        }
    }

    private static byte c = 1;
    private static int e = 0;
    public void tranQueue(Deque queue){
        if(queue == null){
         return;
        }
        int var2 = c == 1?(AppInfo.packed_data_capacity << 5) * 3: AppInfo.packed_data_capacity << 5;
        int count = queue.size();
        while (e<var2 && count > 0 && sendDataQueue.size() < sendMaxLength){
            byte[] var4 = (byte[])queue.poll();
            --count;
            if(var4 != null){
                if(var2 - e < var4.length) {
                    queue.offerFirst(var4);
                    return;
                }
                sendDataQueue.offer(var4);
                e+=var4.length;
            }
        }
    }

    /**
     * 将未发送的socke包给tempBuffer中
     */
    public void UpLoadToTempBuffer(){
        //TODO uploadingDatas -> tempDataQueue;
    }

    ///------------------------http 缓存
    private Deque  iLocationDeque = new LinkedList();
    public Deque  iLocationTemp = new LinkedList();
    public Deque getiLocations() {
        return iLocationDeque;
    }

    public void setiLocations(Object object) {
        if(iLocationDeque == null){
            return;
        }
        this.iLocationDeque.add(object);
    }

    public void clearILocation(){
        iLocationDeque.clear();
    }
}
